using System.Windows.Forms;
using OhioState.AssetManaged;
using OhioState.Controls;
using OhioState.Graphics;
using OhioState.Math;
using Tao.OpenGl;

namespace ComponentControls
{
    public partial class LightControl : UserControl
    {
        private ExaminerMouseHandler mouseController;
        private Camera camera;
        public Camera Camera
        {
            get { return camera; }
            set { camera = value; }
        }
        
        public LightControl()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            //fix: if we don't set color here, we'll get NullPointer during rendering
            openGLPanel1.BackgroundColor = new OSUColor(0.0f, 0.0f, 0.0f);
            //end of fix
            openGLPanel1.renderScene += new OhioState.Graphics.RenderScene(RenderScene);
            camera = new Camera(70, 1, 0.1f, 100);
            mouseController = new ExaminerMouseHandler();//openGLPanel1, false);
            mouseController.OwnerControl = openGLPanel1;
            mouseController.AddSlave(camera as ITransform);
            camera.RotationCenter = new Vector3(0.0f, 0.0f, 0.0f);
            camera.AddChangingEventHandler(new IAssetManagedHandler(CameraChangingHandler));
        }

        public event LightChangedDelegate lightChangedEvent;
        public delegate void LightChangedDelegate(IAssetManaged managed);

        private void CameraChangingHandler(IAssetManaged managed)
        {
            openGLPanel1.Invalidate();
            lightChangedEvent.Invoke(managed);
        }

        private void RenderScene(OhioState.Graphics.IRenderPanel panel)
        {
            Gl.glClearColor(0.0f, 0.0f, 0.4f, 1.0f);
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
            Gl.glViewport(0, 0, panel.Width, panel.Height);

            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            Glu.gluPerspective(70, 1, 0.1, 100);
            //Gl.glLoadMatrixf(camera.GetProjectionMatrix().GetStreamColumnOrder());
            
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();
            Gl.glTranslatef(0, 0, -27);
            //Gl.glLoadMatrixf(camera.GetModelViewMatrix().GetStreamColumnOrder());

            Gl.glEnable(Gl.GL_LIGHTING);
            Gl.glEnable(Gl.GL_LIGHT0);

            float [] diffuse = {1.0f, 1.0f, 1.0f, 1.0f};
            Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_DIFFUSE, diffuse);

            float[] specular = { 1.0f, 1.0f, 1.0f, 1.0f };
            Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_DIFFUSE, specular);

            Matrix4 matrix = camera.GetModelViewMatrix();
            Vector4 vector = new Vector4(0, 0, 1, 0);
            vector = matrix * vector;
            float[] position = { vector.x, vector.y, vector.z, 0 };
            Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_POSITION, position);
            
            Glu.GLUquadric myQuad = Glu.gluNewQuadric();
            Glu.gluSphere(myQuad, 10, 20, 20);
        }
    }
}
